package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaInternalSalePriceDao;
import com.qymerge.manage.base.ro.MetaInternalSettPriceRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *<p>Title: MetaInternalSettPriceDaoImpl</p>
 *<p>Description: 产品内部结算价</p>
 *
 * @author Xiao
 * @date 2025/5/2 01:23
 * @version 1.0.0
 */
@Repository
public class MetaInternalSalePriceDaoImpl extends BaseDao implements IMetaInternalSalePriceDao {

    private String dwd_MCRS_MDM_meta_InternalSalePrice = "SELECT Id,MaterialId,Price,PeriodYear,BeginPeriodNumber,EndPeriodNumber,Remark FROM dwd_MCRS_MDM_meta_InternalSalePrice WHERE 1=1 ";

    @Override
    public List list(MetaInternalSettPriceRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_InternalSalePrice;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getPeriodYear() > 0) {
            sql += " and PeriodYear=:PeriodYear";
            paramMap.put("PeriodYear", ro.getPeriodYear());
        }
        if (ro.getBeginPeriodNumber() > 0) {
            sql += " and BeginPeriodNumber >= :BeginPeriodNumber";
            paramMap.put("BeginPeriodNumber", ro.getBeginPeriodNumber());
        }
        if (ro.getEndPeriodNumber() > 0) {
            sql += " and EndPeriodNumber <= :EndPeriodNumber";
            paramMap.put("EndPeriodNumber", ro.getEndPeriodNumber());
        }

        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaInternalSettPriceRO>(MetaInternalSettPriceRO.class));
    }

    @Override
    public int total(MetaInternalSettPriceRO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_InternalSalePrice where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getPeriodYear() > 0) {
            sql += " and PeriodYear=:PeriodYear";
            paramMap.put("PeriodYear", ro.getPeriodYear());
        }
        if (ro.getBeginPeriodNumber() > 0) {
            sql += " and BeginPeriodNumber >= :BeginPeriodNumber";
            paramMap.put("BeginPeriodNumber", ro.getBeginPeriodNumber());
        }
        if (ro.getEndPeriodNumber() > 0) {
            sql += " and EndPeriodNumber <= :EndPeriodNumber";
            paramMap.put("EndPeriodNumber", ro.getEndPeriodNumber());
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public MetaInternalSettPriceRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_InternalSalePrice);
        sql.append("and Id=?");
        RowMapper<MetaInternalSettPriceRO> rowMapper = new BeanPropertyRowMapper<MetaInternalSettPriceRO>(MetaInternalSettPriceRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }


}
